package com.paddyi.controller;//package com.springcloud.controller;

import com.paddyi.commons.utils.StringEscapeEditor;
import com.paddyi.model.SsoUser;
import com.paddyi.service.ISsoUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 *
 * ClsTradeOrder 控制层
 *
 */
public class BaseController {

    @Autowired
    private ISsoUserService userService;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 自动转换日期类型的字段格式
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
        /**
         * 防止XSS攻击
         */
        binder.registerCustomEditor(String.class, new StringEscapeEditor());
    }


    /**
     * 获取当前登录用户对象
     * @return
     */
    public SsoUser getCurrentUser() {
        SsoUser user = (SsoUser) SecurityUtils.getSubject().getPrincipal();
        SsoUser currentUser = userService.getById(user.getId());
        currentUser.setPlaintext("");
        currentUser.setPassword("");
        return currentUser;
    }

    /**
     * 获取当前登录用户id
     * @return
     */
    public Long getUserId() {
        return this.getCurrentUser().getId();
    }

    /**
     * 获取当前登录用户名
     * @return
     */
    public String getUsereName() {
        return this.getCurrentUser().getName();
    }

}